home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / exec / RCS / execvp.c,v < prev   
Encoding:
Text File  |  1992-01-27  |  5.9 KB  |  305 lines

  1. head     1.5;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.5.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.5
  10. date     91.07.22.10.42.58;  author ouster;  state Exp;
  11. branches 1.5.1.1;
  12. next     1.4;
  13.  
  14. 1.4
  15. date     91.07.22.10.22.12;  author ouster;  state Exp;
  16. branches ;
  17. next     1.3;
  18.  
  19. 1.3
  20. date     88.07.28.17.50.10;  author ouster;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     88.06.21.10.20.09;  author ouster;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     88.06.19.16.56.01;  author ouster;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34. 1.5.1.1
  35. date     92.01.26.17.17.19;  author kupfer;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 1.5
  45. log
  46. @Found another malloc and another free to eliminate.
  47. @
  48. text
  49. @/* 
  50.  * execvp.c --
  51.  *
  52.  *    Source code for the execvp library procedure.
  53.  *
  54.  * Copyright 1988 Regents of the University of California
  55.  * Permission to use, copy, modify, and distribute this
  56.  * software and its documentation for any purpose and without
  57.  * fee is hereby granted, provided that the above copyright
  58.  * notice appear in all copies.  The University of California
  59.  * makes no representations about the suitability of this
  60.  * software for any purpose.  It is provided "as is" without
  61.  * express or implied warranty.
  62.  */
  63.  
  64. #ifndef lint
  65. static char rcsid[] = "$Header: execvp.c,v 1.3 88/07/28 17:50:10 ouster Exp $ SPRITE (Berkeley)";
  66. #endif not lint
  67.  
  68. #include <string.h>
  69. #include <stdlib.h>
  70. #include <errno.h>
  71.  
  72. /*
  73.  * Library imports:
  74.  */
  75.  
  76. extern char **environ;
  77. extern execve();
  78.  
  79.  
  80. /*
  81.  *-----------------------------------------------------------------------
  82.  *
  83.  * DoExec --
  84.  *
  85.  *    Function to actually execute a program. If the exec didn't succeed
  86.  *    because the file isn't in a.out format, attempt to execute
  87.  *    it as a bourne shell script.
  88.  *
  89.  * Results:
  90.  *    None.  Doesn't even return unless the exec failed.
  91.  *
  92.  * Side Effects:
  93.  *    A program may be execed over this one.
  94.  *
  95.  *-----------------------------------------------------------------------
  96.  */
  97.  
  98. static void
  99. DoExec(file, argv)
  100.     char *file;            /* File to execute. */
  101.     char **argv;        /* Arguments to the program. */
  102. {
  103.     execve(file, argv, environ);
  104.     if (errno == ENOEXEC) {
  105.     /*
  106.      * Attempt to execute the file as a shell script using
  107.      * the Bourne shell)
  108.      */
  109.     register int i;
  110. #define MAX_ARGS 1000
  111.     char *newArgv[MAX_ARGS+1];
  112.  
  113.     for (i = 0; argv[i] != 0; i++) {
  114.         /* Empty loop body */
  115.     }
  116.     if (i >= MAX_ARGS) {
  117.         return;
  118.     }
  119.     newArgv[0] = "sh";
  120.     newArgv[1] = file;
  121.     for (i = 1; argv[i] != 0; i++) {
  122.         newArgv[i+1] = argv[i];
  123.     }
  124.     newArgv[i+1] = 0;
  125.     execve("/sprite/cmds/sh", newArgv, environ);
  126.     }
  127. }
  128.  
  129. /*
  130.  *----------------------------------------------------------------------
  131.  *
  132.  * execvp --
  133.  *
  134.  *    Execute a process, using the current environment variable,
  135.  *    instead of an explicitly-supplied one.  Also, imitate the
  136.  *    shell's actions in trying each directory in a search path
  137.  *    (given by the "PATH" environment variable).
  138.  *
  139.  * Results:
  140.  *    This procedure returns only if the exec fails.  In this case
  141.  *    the return value is -1.
  142.  *
  143.  * Side effects:
  144.  *    Overlays the current process with a new image.  See the man
  145.  *    page for details.
  146.  *
  147.  *----------------------------------------------------------------------
  148.  */
  149.  
  150. int
  151. execvp(name, argv)
  152.     char *name;            /* Name of file containing program to exec. */
  153.     char **argv;        /* Array of arguments to pass to program. */
  154. {
  155.     char *path;
  156. #define MAX_NAME_SIZE 1000
  157.     char fullName[MAX_NAME_SIZE+1];
  158.     register char *first, *last;
  159.     int nameLength, size, noAccess;
  160.  
  161.     noAccess = 0;
  162.  
  163.     if (index(name, '/') != 0) {
  164.     /*
  165.      * If the name specifies a path, don't search for it on the search path,
  166.      * just try and execute it.
  167.      */
  168.     DoExec(name, argv);
  169.     return -1;
  170.     }
  171.  
  172.     path = getenv("PATH");
  173.     if (path == 0) {
  174.     path = "/sprite/cmds";
  175.     }
  176.     nameLength = strlen(name);
  177.     for (first = path; ; first = last+1) {
  178.  
  179.     /*
  180.      * Generate the next file name to try.
  181.      */
  182.  
  183.     for (last = first; (*last != 0) && (*last != ':'); last++) {
  184.         /* Empty loop body. */
  185.     }
  186.     size = last-first;
  187.     if ((size + nameLength + 2) >= MAX_NAME_SIZE) {
  188.         continue;
  189.     }
  190.     (void) strncpy(fullName, first, size);
  191.     if (last[-1] != '/') {
  192.         fullName[size] = '/';
  193.         size++;
  194.     }
  195.     (void) strcpy(fullName + size, name);
  196.  
  197.     DoExec(fullName, argv);
  198.     if (errno == EACCES) {
  199.         noAccess = 1;
  200.     } else if (errno != ENOENT) {
  201.         break;
  202.     }
  203.     if (*last == 0) {
  204.         /*
  205.          * Hit the end of the path. We're done.
  206.          * If there existed a file by the right name along the search path,
  207.          * but its permissions were wrong, return FS_NO_ACCESS. Else return
  208.          * whatever we just got back.
  209.          */
  210.         if (noAccess) {
  211.         errno = EACCES;
  212.         }
  213.         break;
  214.     }
  215.     }
  216.     return -1;
  217. }
  218. @
  219.  
  220.  
  221. 1.5.1.1
  222. log
  223. @Initial branch for Sprite server.
  224. @
  225. text
  226. @d17 1
  227. a17 1
  228. static char rcsid[] = "$Header: /sprite/src/lib/c/exec/RCS/execvp.c,v 1.5 91/07/22 10:42:58 ouster Exp $ SPRITE (Berkeley)";
  229. @
  230.  
  231.  
  232. 1.4
  233. log
  234. @Can't use malloc:  after vfork, memory can be shared with parent,
  235. so malloc causes parent to lose memory.
  236. @
  237. text
  238. @a60 1
  239.     register char **newargv;
  240. d62 2
  241. d68 5
  242. a72 3
  243.     newargv = (char **) malloc((unsigned) ((i+1)*sizeof (char *)));
  244.     newargv[0] = "sh";
  245.     newargv[1] = file;
  246. d74 1
  247. a74 1
  248.         newargv[i+1] = argv[i];
  249. d76 2
  250. a77 2
  251.     newargv[i+1] = 0;
  252.     execve("/sprite/cmds/sh", newargv, environ);
  253. a167 1
  254.     free((char *) fullName);
  255. @
  256.  
  257.  
  258. 1.3
  259. log
  260. @Lint.
  261. @
  262. text
  263. @d17 1
  264. a17 1
  265. static char rcsid[] = "$Header: execvp.c,v 1.2 88/06/21 10:20:09 ouster Exp $ SPRITE (Berkeley)";
  266. d105 2
  267. a106 1
  268.     char *fullName;
  269. d108 1
  270. a108 1
  271.     int size, noAccess;
  272. d125 1
  273. a125 1
  274.     fullName = malloc((unsigned) (strlen(name) + strlen(path)) + 2);
  275. d136 3
  276. @
  277.  
  278.  
  279. 1.2
  280. log
  281. @Use string.h instead of strings.h.
  282. @
  283. text
  284. @d17 1
  285. a17 1
  286. static char rcsid[] = "$Header: execvp.c,v 1.1 88/06/19 16:56:01 ouster Exp $ SPRITE (Berkeley)";
  287. d116 1
  288. a116 1
  289.     DoExec(name, argv, environ);
  290. @
  291.  
  292.  
  293. 1.1
  294. log
  295. @Initial revision
  296. @
  297. text
  298. @d17 1
  299. a17 1
  300. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  301. d20 1
  302. a20 1
  303. #include <strings.h>
  304. @
  305.